package com.tcm.controller.backend;

import com.tcm.common.enums.ResponseEnum;
import com.tcm.entity.dto.param.PermissionRoleUpdateParam;
import com.tcm.entity.resp.PageR;
import com.tcm.entity.vo.PermissionListResult;
import com.tcm.entity.resp.R;
import com.tcm.entity.vo.RoleSelectResult;
import com.tcm.service.PermissionService;
import com.tcm.utils.AccessTokenUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 权限管理控制器
 * @author Guqier
 * @version 1.0
 * @date 2023/7/12 17:42
 */
@Slf4j
@RestController
@RequestMapping("/pc/permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    /**
     * 分页获取角色列表
     * @param page 当前页号
     * @param pageSize 页面大小
     * @return 响应结果
     */
    @RequestMapping(value = "/roleList", method = RequestMethod.GET)
    public R getRoleListByPage(Integer page, Integer pageSize){
        if (page == null || pageSize == null) {
            return R.fail(ResponseEnum.PARAM_IS_BLANK);
        }
        PageR<RoleSelectResult> roleListByPage = permissionService.getRoleListByPage(page, pageSize);
        return R.success(roleListByPage);
    }

    /**
     * 获取权限列表
     * @return 响应结果
     */
    @RequestMapping(method = RequestMethod.GET)
    public R getPermissionList(){
        List<PermissionListResult> permissionList = permissionService.getPermissionList();
        return R.success(permissionList);
    }

    /**
     * 根据角色ID获取角色权限
     * @param roleId 角色ID
     * @return 角色权限列表响应
     */
    @RequestMapping(value = "/role", method = RequestMethod.GET)
    public R getPermissionByRoleId(Long roleId){
        if (roleId == null) {
            return R.fail(ResponseEnum.PARAM_IS_BLANK);
        }
        List<PermissionListResult> permissionByRoleId = permissionService.getPermissionByRoleId(roleId);
        return R.success(permissionByRoleId);
    }

    /**
     * 修改角色权限
     * @param param 参数封装对象
     * @param request http请求对象
     * @return 响应结果
     */
    @RequestMapping(method = RequestMethod.PUT)
    public R modifyPermissionForRole(@RequestBody @Valid PermissionRoleUpdateParam param, HttpServletRequest request){
        boolean res = permissionService.modifyPermissionForRole(param, AccessTokenUtil.getUserId(request));
        return res ? R.success() : R.fail(ResponseEnum.PARAM_IS_INVAlID);
    }

    /**
     * 添加角色
     * @param param 参数map
     * @return 响应结果
     */
    @RequestMapping(method = RequestMethod.POST)
    public R addRole(@RequestBody Map<String, String> param, HttpServletRequest request){
        if (!StringUtils.hasText(param.get("roleKey")) || !StringUtils.hasText(param.get("roleName")) || !StringUtils.hasText(param.get("des"))) {
            return R.fail(ResponseEnum.PARAM_IS_BLANK);
        }
        boolean res = permissionService.addRole(param.get("roleKey"), param.get("roleName"), param.get("des"), AccessTokenUtil.getUserId(request));
        return res ? R.success() : R.fail(ResponseEnum.DATABASE_OPERATION_FAIL);
    }

    /**
     * 删除角色
     * @param id 角色ID
     * @param request http请求对象
     * @return 处理结果
     */
    @RequestMapping(method = RequestMethod.DELETE)
    public R removeRole(Long id, HttpServletRequest request){
        if (id == null) {
            return R.fail(ResponseEnum.PARAM_IS_BLANK);
        }
        boolean res = false;
        try {
            res = permissionService.removeRole(id, AccessTokenUtil.getUserId(request));
        } catch (Exception e) {
            return R.fail(0, e.getMessage());
        }
        return res ? R.success() : R.fail(ResponseEnum.PARAM_IS_INVAlID);
    }

}
